package etr.bk.db;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.swing.JOptionPane;

import org.h2.jdbc.JdbcSQLException;

public class ConnectDB 
{
	private Connection conn;
	
	public ConnectDB(String propName)
	{
		String prop = propName;
		
		Properties proper = new Properties();
		
		try
		{
			FileInputStream inProper = new FileInputStream("prop/"+prop);
			proper.load(inProper);
			inProper.close();
			
			
			String driverName = proper.getProperty("driver.name");
			
			Class.forName(driverName);
			
			String url = proper.getProperty("url");
			String user = proper.getProperty("user");
			String pass = proper.getProperty("pass");
			
			this.conn = DriverManager.getConnection(url, user, pass);
			
		}
		catch (FileNotFoundException e1)
		{
			System.out.println("------Prop FILE NOT FOUND----");
			e1.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
				e.printStackTrace();
				System.out.println("------Driver NOT FOUND----");
		}
		catch (JdbcSQLException u)
		{
			System.out.println("SUCHARABLYA!!!");
			JOptionPane.showMessageDialog(null,
				    "Application already start!",
				    "ERROR",
				    JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public Connection getConnection()
	{
		return this.conn;
	}
	
	/*protected void finalize()
	{
		try
		{
			this.conn.close();
		}
		catch (SQLException e)
		{
			
			e.printStackTrace();
		}
	}*/
}
